using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.ACTP.Components
{
	public partial class Project
	{
		#region Properties.
		
		public int ProjectID { set; get; }
		public Guid ModuleID { set; get; }
		public int TeamID { set; get; }
		public int ProjectTypeID { set; get; }
		public string Code { set; get; }
		public string Subject { set; get; }
		public DateTime StartDate { set; get; }
		public DateTime DueDate { set; get; }
		public DateTime CompletedDate { set; get; }
		public string Priority { set; get; }
		public string Status { set; get; }
		public byte CompletedPercent { set; get; }
		public string Description { set; get; }
		public string CreatedBy { set; get; }
		public DateTime CreatedDate { set; get; }
		public string UpdatedBy { set; get; }
		public DateTime UpdatedDate { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static IList<Project> ConvertToCollection(IDataReader reader)
		{
			IList<Project> collection = new List<Project>();
			while (reader.Read())
			{
				Project entity = new Project();
				if (!reader.IsDBNull(reader.GetOrdinal("ProjectID"))) entity.ProjectID = reader.GetInt32(reader.GetOrdinal("ProjectID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TeamID"))) entity.TeamID = reader.GetInt32(reader.GetOrdinal("TeamID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ProjectTypeID"))) entity.ProjectTypeID = reader.GetInt32(reader.GetOrdinal("ProjectTypeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Code"))) entity.Code = reader.GetString(reader.GetOrdinal("Code"));
				if (!reader.IsDBNull(reader.GetOrdinal("Subject"))) entity.Subject = reader.GetString(reader.GetOrdinal("Subject"));
				if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) entity.StartDate = reader.GetDateTime(reader.GetOrdinal("StartDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("DueDate"))) entity.DueDate = reader.GetDateTime(reader.GetOrdinal("DueDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("CompletedDate"))) entity.CompletedDate = reader.GetDateTime(reader.GetOrdinal("CompletedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("Priority"))) entity.Priority = reader.GetString(reader.GetOrdinal("Priority"));
				if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
				if (!reader.IsDBNull(reader.GetOrdinal("CompletedPercent"))) entity.CompletedPercent = reader.GetByte(reader.GetOrdinal("CompletedPercent"));
				if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedBy"))) entity.UpdatedBy = reader.GetString(reader.GetOrdinal("UpdatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) entity.UpdatedDate = reader.GetDateTime(reader.GetOrdinal("UpdatedDate"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Project Load(int projectID)
		{
			const string spName = "[dbo].[p_ACTP_Project_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, projectID);
            IDataReader reader = db.ExecuteReader(dbCommand);
			IList<Project> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static IList<Project> SelectCollectionAll(Guid moduleID)
		{
			IDataReader reader = SelectReaderAll(moduleID);
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static IList<Project> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IList<Project> SelectCollectionBy_ProjectTypeID(int projectTypeID)
		{
            IDataReader reader = SelectReaderBy_ProjectTypeID(projectTypeID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		public static IList<Project> SelectCollectionBy_TeamID(int teamID)
		{
            IDataReader reader = SelectReaderBy_TeamID(teamID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_ProjectTypeID(int projectTypeID)
		{
			const string spName = "[dbo].[p_ACTP_Project_SelectBy_ProjectTypeID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, projectTypeID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------
		public static DataSet SelectBy_TeamID(int teamID)
		{
			const string spName = "[dbo].[p_ACTP_Project_SelectBy_TeamID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, teamID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Project_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Project_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Project_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Project_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_ProjectTypeID(int projectTypeID)
		{
			const string spName = "p_ACTP_Project_SelectBy_ProjectTypeID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, projectTypeID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		public static IDataReader SelectReaderBy_TeamID(int teamID)
		{
			const string spName = "p_ACTP_Project_SelectBy_TeamID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, teamID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertProject(Guid moduleID, int teamID, int projectTypeID, string code, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string priority, string status, byte completedPercent, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Project entity = new Project();	
			entity.ModuleID = moduleID;
			entity.TeamID = teamID;
			entity.ProjectTypeID = projectTypeID;
			entity.Code = code;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Priority = priority;
			entity.Status = status;
			entity.CompletedPercent = completedPercent;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_ACTP_Project_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ProjectID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, TeamID);
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, ProjectTypeID);
			db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ProjectID = (int) db.GetParameterValue(dbCommand, "@ProjectID");
				return ProjectID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ProjectID = (int) db.GetParameterValue(dbCommand, "@ProjectID");
				return ProjectID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<Project> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Project item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateProject(int projectID, Guid moduleID, int teamID, int projectTypeID, string code, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string priority, string status, byte completedPercent, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Project entity = new Project();			
			entity.ProjectID = projectID;
			entity.ModuleID = moduleID;
			entity.TeamID = teamID;
			entity.ProjectTypeID = projectTypeID;
			entity.Code = code;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Priority = priority;
			entity.Status = status;
			entity.CompletedPercent = completedPercent;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_ACTP_Project_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, ProjectID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, TeamID);
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, ProjectTypeID);
			db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<Project> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Project item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateProject(int projectID, Guid moduleID, int teamID, int projectTypeID, string code, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string priority, string status, byte completedPercent, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate)
		{
			Project entity = new Project();			
			entity.ProjectID = projectID;
			entity.ModuleID = moduleID;
			entity.TeamID = teamID;
			entity.ProjectTypeID = projectTypeID;
			entity.Code = code;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Priority = priority;
			entity.Status = status;
			entity.CompletedPercent = completedPercent;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Project_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, ProjectID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, TeamID);
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, ProjectTypeID);
			db.AddInParameter(dbCommand, "@Code", SqlDbType.NVarChar, Code);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<Project> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Project item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteProject(int projectID)
		{
			Project entity = new Project();
			entity.ProjectID = projectID;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Project_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, ProjectID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_ProjectTypeID(int projectTypeID)
		{
			const string spName = "[dbo].[p_ACTP_Project_DeleteBy_ProjectTypeID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ProjectTypeID", SqlDbType.Int, projectTypeID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		public static int DeleteBy_TeamID(int teamID)
		{
			const string spName = "[dbo].[p_ACTP_Project_DeleteBy_TeamID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TeamID", SqlDbType.Int, teamID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_ACTP_Project_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<Project> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Project item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}